package zhihui.mobile.presenter.shop;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.page.PageInfo;
import zhihui.mobile.bean.shop.CategoryShopInfo;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.shop.ShopManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.list.IShopListContract;

/**
 * Created by 张公铭 on 2017/8/2 0002.
 */

public class ShopListPresenter extends BaseShopPresenter implements IShopListContract.IShopListPresenter
{
    private Context mContext;
    private IShopListContract.IShopListView shopListView;


    @Override
    public void init(IView view, Context context)
    {
        mContext = context;
        shopListView = (IShopListContract.IShopListView) view;
    }

    @Override
    public void onResume()
    {
        shopListView.updateCartNum(ShopManager.getInstance().getCartInfo().getCartNum());
    }

    @Override
    public void onStart()
    {

    }

    @Override
    public void onPause()
    {

    }

    @Override
    public void onStop()
    {

    }

    @Override
    public void onDestroy()
    {

    }

    @Override
    public void initData()
    {
        shopListView.initView(ShopManager.getInstance().getCategoryShopInfo().getList());
    }

    @Override
    public void refresh(String descType, String desc)
    {
        shopListView.showLoading();
        ShopManager.getInstance().getShopListByCategoryId(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                Gson gson = new Gson();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    PageInfo pageInfo = gson.fromJson(data.toString(), PageInfo.class);
                    List<SpuInfo> list = gson.fromJson(data.getJSONArray("list").toString(), new TypeToken<List<SpuInfo>>()
                    {
                    }.getType());
                    CategoryShopInfo categoryShopInfo = new CategoryShopInfo();
                    categoryShopInfo.setParam(ShopManager.getInstance().getCategoryShopInfo().getParam());
                    categoryShopInfo.setPageInfo(pageInfo);
                    categoryShopInfo.setList(list);
                    ShopManager.getInstance().setCategoryShopInfo(categoryShopInfo);
                    shopListView.refreshList(list);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    shopListView.showMessage(mContext.getString(R.string.json_error));
                    shopListView.onRefreshComplete();
                }
                shopListView.hindLoading();
            }

            @Override
            public void failed(String message)
            {
                shopListView.hindLoading();
                shopListView.showMessage(mContext.getString(R.string.http_error));
                shopListView.onRefreshComplete();
            }
        }, ShopManager.getInstance().getCategoryShopInfo().getParam(), 1, descType, desc);
    }

    @Override
    public void loadmore(String descType, String desc)
    {
        PageInfo pageInfo = ShopManager.getInstance().getCategoryShopInfo().getPageInfo();
        shopListView.showLoading();
        ShopManager.getInstance().getShopListByCategoryId(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                Gson gson = new Gson();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    PageInfo pageInfo = gson.fromJson(data.toString(), PageInfo.class);
                    List<SpuInfo> list = gson.fromJson(data.getJSONArray("list").toString(), new TypeToken<List<SpuInfo>>()
                    {
                    }.getType());
                    ShopManager.getInstance().getCategoryShopInfo().setPageInfo(pageInfo);
                    ShopManager.getInstance().getCategoryShopInfo().getList().addAll(list);
                    shopListView.addShopList(list);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    shopListView.showMessage(mContext.getString(R.string.json_error));
                    shopListView.onRefreshComplete();
                }
                shopListView.hindLoading();
            }

            @Override
            public void failed(String message)
            {
                shopListView.hindLoading();
                shopListView.showMessage(mContext.getString(R.string.http_error));
                shopListView.onRefreshComplete();
            }
        }, ShopManager.getInstance().getCategoryShopInfo().getParam(), pageInfo.getPageNumber() + 1, descType, desc);
    }

    @Override
    public void searchRefresh(String descType, String desc)
    {
        shopListView.showLoading();
        ShopManager.getInstance().getShopListBySearch(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                Gson gson = new Gson();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    PageInfo pageInfo = gson.fromJson(data.toString(), PageInfo.class);
                    List<SpuInfo> list = gson.fromJson(data.getJSONArray("list").toString(), new TypeToken<List<SpuInfo>>()
                    {
                    }.getType());
                    CategoryShopInfo categoryShopInfo = new CategoryShopInfo();
                    categoryShopInfo.setParam(ShopManager.getInstance().getCategoryShopInfo().getParam());
                    categoryShopInfo.setPageInfo(pageInfo);
                    categoryShopInfo.setList(list);
                    ShopManager.getInstance().setCategoryShopInfo(categoryShopInfo);
                    shopListView.refreshList(list);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    shopListView.showMessage(mContext.getString(R.string.json_error));
                    shopListView.onRefreshComplete();
                }
                shopListView.hindLoading();
            }

            @Override
            public void failed(String message)
            {
                shopListView.hindLoading();
                shopListView.showMessage(mContext.getString(R.string.http_error));
                shopListView.onRefreshComplete();
            }
        }, ShopManager.getInstance().getCategoryShopInfo().getParam(), 1, descType, desc);
    }

    @Override
    public void searchLoadmore(String descType, String desc)
    {
        PageInfo pageInfo = ShopManager.getInstance().getCategoryShopInfo().getPageInfo();
        shopListView.showLoading();
        ShopManager.getInstance().getShopListBySearch(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                Gson gson = new Gson();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    PageInfo pageInfo = gson.fromJson(data.toString(), PageInfo.class);
                    List<SpuInfo> list = gson.fromJson(data.getJSONArray("list").toString(), new TypeToken<List<SpuInfo>>()
                    {
                    }.getType());
                    ShopManager.getInstance().getCategoryShopInfo().setPageInfo(pageInfo);
                    ShopManager.getInstance().getCategoryShopInfo().getList().addAll(list);
                    shopListView.addShopList(list);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    shopListView.showMessage(mContext.getString(R.string.json_error));
                    shopListView.onRefreshComplete();
                }
                shopListView.hindLoading();
            }

            @Override
            public void failed(String message)
            {
                shopListView.hindLoading();
                shopListView.showMessage(mContext.getString(R.string.http_error));
                shopListView.onRefreshComplete();
            }
        }, ShopManager.getInstance().getCategoryShopInfo().getParam(), pageInfo.getPageNumber() + 1, descType, desc);
    }

    @Override
    public void initSearchText()
    {
        shopListView.updateSearchText(ShopManager.getInstance().getCategoryShopInfo().getParam());
    }

    @Override
    public void search(final String text)
    {
        shopListView.showLoading();
        ShopManager.getInstance().getShopListBySearch(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                Gson gson = new Gson();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    PageInfo pageInfo = gson.fromJson(data.toString(), PageInfo.class);
                    List<SpuInfo> list = gson.fromJson(data.getJSONArray("list").toString(), new TypeToken<List<SpuInfo>>()
                    {
                    }.getType());
                    CategoryShopInfo categoryShopInfo = new CategoryShopInfo();
                    categoryShopInfo.setParam(text);
                    categoryShopInfo.setPageInfo(pageInfo);
                    categoryShopInfo.setList(list);
                    ShopManager.getInstance().setCategoryShopInfo(categoryShopInfo);
                    shopListView.refreshList(list);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    shopListView.showMessage(mContext.getString(R.string.json_error));
                    shopListView.onRefreshComplete();
                }
                shopListView.hindLoading();
            }

            @Override
            public void failed(String message)
            {
                shopListView.hindLoading();
                shopListView.showMessage(mContext.getString(R.string.http_error));
                shopListView.onRefreshComplete();
            }
        }, text, 1, "", "");
    }
}
